package com.financing.wap.web.controller.api;

import java.math.BigDecimal;
import java.net.HttpURLConnection;
import java.net.URL;
import java.net.URLConnection;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;

import org.apache.commons.io.IOUtils;
import org.apache.log4j.Logger;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import com.financing.wap.common.Pager;
import com.financing.wap.common.ResultMap;
import com.financing.wap.dto.CompositeIndexLogDTO;
import com.financing.wap.service.ICompositeIndexLogService;
import com.financing.wap.web.controller.BaseController;

@Controller("compositeIndexLogController")
@RequestMapping("/api/compositeIndexLog")
public class CompositeIndexLogController extends BaseController  { 

	public static List<String> dapan=new ArrayList<String>();
	
	static{
		dapan.add("s_sh000001,上证指数");// 下午3点后收盘
		dapan.add("s_sz399001,深证成指");// 下午3点后收盘
		dapan.add("s_sz399006,创业板指");// 下午3点后收盘
//		dapan.add("int_hangseng,恒生指数");//下午4点
//		dapan.add("int_sp500,标普500");//凌晨4点
//		dapan.add("int_nasdaq,纳斯达克");//凌晨4点
//		dapan.add("int_nikkei,日经225");//下午2点
//		dapan.add("int_dax30,德国DAX");//下午11点半
	}
	
	private Logger logger = Logger.getLogger(getClass());
	
	@Autowired
	private ICompositeIndexLogService compositeIndexLogService;
	
	@RequestMapping("/listCompositeIndexLog")
	@ResponseBody
	public ResultMap list(HttpServletRequest request, CompositeIndexLogDTO compositeIndexLog,Pager pager){
		ResultMap result = new ResultMap();
		
		try{
			logger.info("query compositeIndexLog list begin .......");
			
			List<CompositeIndexLogDTO> list = getCompositeIndex(dapan);
			
			result.setAttribute("list", list);
			result.setStatusCode(ResultMap.STATUS_CODE_SUCCESS);
			result.setStatus(ResultMap.STATUS.SUCCESS);
			
			logger.info("query compositeIndexLog list end .......");
		}catch(Exception e){
			log.error("query compositeIndexLog error!",e);
			result.setStatus(ResultMap.STATUS.ERROR);
			result.setStatusCode(ResultMap.STATUS_CODE_SYS_EXCEPTION);
		}
		
		return result;
	}

	/**
	 * 大盘指数
	 * 
	 * @param key
	 */
	private static List<CompositeIndexLogDTO> getCompositeIndex(List<String> dapan){
		List<CompositeIndexLogDTO> list = new ArrayList<CompositeIndexLogDTO>();
		
		URL url;
		try {
			for(String keys:dapan) {
				String key = keys.split(",")[0];
				String value = keys.split(",")[1];
				String initUrl="http://hq.sinajs.cn/list="+key;
				
				url = new URL(initUrl);
				URLConnection openConnection = url.openConnection();
				HttpURLConnection httpConnection=(HttpURLConnection)openConnection;
				if (httpConnection.getResponseCode() >= 300) {
			        throw new Exception("HTTP Request is not success, Response code is " + httpConnection.getResponseCode());
			    }
				String result = IOUtils.toString(httpConnection.getInputStream(),"GBK");
				String[] split = result.split(",");
				String endPrice=split[1];
				String changePrice=split[2];
				String changePriceRate=split[3];
				changePriceRate=changePriceRate.replace("\";", "");
				CompositeIndexLogDTO indexLog=new CompositeIndexLogDTO();
				BigDecimal indexEndPrice = new BigDecimal(endPrice);
				indexLog.setIndexEndPrice(indexEndPrice);
				indexLog.setIndexName(value);
				if(key.matches("^int.*")&&!key.equals("int_hangseng")){
					indexLog.setIndexType("2");
				}else{
					indexLog.setIndexType("1");
				}
				BigDecimal indexChangePrice = new BigDecimal(changePrice);
				indexLog.setIndexChangePrice(indexChangePrice);
				indexLog.setIndexCode(key);
				indexLog.setIndexChangePriceRate(changePriceRate);
				indexLog.setDeleteTag("1");
				list.add(indexLog);
			}
			return list;
		} catch(Exception e){
			log.error("query compositeIndexLog error!",e);
		}
		return null;
	}
}
